import dayjs from 'dayjs'

// 详细化格式
export function formatDateToDetail (date){
    const now = new Date();
    const targetDate = new Date(date);
    const timeDiff = now - targetDate; // 计算时间差

    const seconds = Math.floor(timeDiff / 1000);
    const minutes = Math.floor(seconds / 60);
    const hours = Math.floor(minutes / 60);

    const options = { hour: '2-digit', minute: '2-digit' };

    if (timeDiff < 12 * 60 * 60 * 1000) { // 小于12小时
        if (hours > 0) {
            return `${hours}小时前`;
        } else if (minutes > 0) {
            return `${minutes}分钟前`;
        } else {
            return `${seconds}秒前`;
        }
    } else if (now.toDateString() === targetDate.toDateString()) {
        return targetDate.toLocaleTimeString('zh-CN', options);
    } else if (new Date(now.setDate(now.getDate() - 1)).toDateString() === targetDate.toDateString()) {
        return `昨天 ${targetDate.toLocaleTimeString('zh-CN', options)}`;
    } else {
        return targetDate.toLocaleDateString('zh-CN');
    }
}

export function formatDateToYYMMDDHHMMSS(time) {
    return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}

export function formatDateToYYMMDD(time) {
    return dayjs(time).format('YYYY-MM-DD')
}